<template>
  <el-container class="tab-container">
    <el-main class="main">
      <collapse-tab class="main-search">
        <el-form
          :inline="true"
          :model="queryCondition"
          label-width="80px"
          @keyup.enter.native="query"
        >
          <!--查询条件区 -->
          <el-form-item label="标题">
            <p-input v-model="queryCondition.title" class="form-item" />
          </el-form-item>
          <el-form-item label="是否已读">
            <dictionary-select
              v-model="queryCondition.readFlag"
              multiple
              :code="constant.DICTIONARY_TYPE_CODES.YES_OR_NO"
              class="form-item"
            />
          </el-form-item>
          <el-form-item style="float:right;">
            <list-button-query :page-code="pageCode" />
          </el-form-item>
          <div class="clearfix" />
        </el-form>
      </collapse-tab>
      <el-card class="main-report">
        <div>
          <list-button-refresh :page-code="pageCode" />
          <list-button-remove :page-code="pageCode" />
          <el-button
            v-permission="pageCode+'allRead'"
            type="primary"
            icon="el-icon-check"
            size="small"
            @click="allRead"
          >全部已读</el-button>
          <div style="clear:both;" />
        </div>
        <div style="margin-top:5px;">
          <div>
            <columns-controller v-model="columnList" :table-key="tableKey" />
          </div>
          <el-table
            v-loading="loading"
            :data="tableData"
            style="width: 100%"
            highlight-current-row
            border
            @sort-change="handleSortChange"
            @current-change="handleRowChange"
            @selection-change="handleSelectionChange"
            @row-dblclick="handleDoubleClickRow"
          >
            <el-table-column type="selection" width="55" />
            <el-table-column
              v-for="(item, index) in showCols"
              :key="index"
              :label="item.label"
              :prop="item.prop"
              :show-overflow-tooltip="item.showOverflowTooltip"
              :width="item.width"
              :formatter="item.formatFunc"
              :sortable="item.sortable"
            />
            <el-table-column fixed="right" label="操作" width="250">
              <template slot-scope="scope">
                <table-button-view :id="scope.row.id" :page-code="pageCode" />
                <table-button-remove :id="scope.row.id" :page-code="pageCode" />
              </template>
            </el-table-column>
          </el-table>
        </div>
        <list-pager
          :page-num="pageInfo.pageNum"
          :page-size="pageInfo.pageSize"
          :page-total="pageTotal"
        />
      </el-card>
      <detail ref="detail" @ok="handleDetailSave" />
    </el-main>
  </el-container>
</template>

<script>
import { listMixin } from '@/mixin/listMixin'
import Detail from './detail'

const MODULE_CODE = 'support'
const ENTITY_TYPE = 'message'
export default {
  name: ENTITY_TYPE,
  components: { Detail },
  mixins: [listMixin],
  data() {
    return {
      entityType: ENTITY_TYPE,
      moduleCode: MODULE_CODE,
      // eslint-disable-next-line no-eval
      api: eval('this.$api.' + MODULE_CODE + '.' + ENTITY_TYPE),
      pageCode: MODULE_CODE + ':' + ENTITY_TYPE + ':',
      queryCondition: {},
      columnList: [
        // prop label show 必须定义， width/showOverflowTooltip/formatFunc/sortable 需要的话定义
        { prop: 'title', label: '消息标题', show: true },
        { prop: 'createTime', label: '创建时间', show: true, formatFunc: this.tableDateFormatter.formatTime },
        { prop: 'readFlagName', label: '是否已读', show: true }

      ],
      sortInfo: {
        sort_field: 'createTime',
        sort_sortType: 'descending'
      }
    }
  },
  methods: {

    handleDoubleClickRow(row) {
      this.$refs.detail.view(row.id)
    },
    allRead() {
      this.api.allRead().then(() => {
        // 未读消息清零
        this.$store.dispatch('app/setUnreadMessageCountEqaulZero')
        this.refresh()
      })
    },
    // 批量移除
    batchRemove() {
      if (this.multipleSelection.length === 0) {
        this.$message({
          type: 'info',
          message: '请至少选中一行'
        })
        return
      }
      this.$confirm('此操作将批量删除数据, 是否继续?', '确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const ids = this.getCheckedId()
        this.api.remove(ids)
          .then(() => {
            // 重新读取未读消息数量
            this.$store.dispatch('app/initUnreadMessageCount')
            this.loadData()
          })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        })
      })
    },
    // 移除
    remove(id) {
      this.$confirm('此操作将删除数据, 是否继续?', '确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.api.remove(id)
          .then(() => {
            // 重新读取未读消息数量,此处实际应该判断消息是否未读，但传来的参数只有标识，还需要再查询数据，因此直接重新获取
            this.$store.dispatch('app/initUnreadMessageCount')
            this.loadData()
          })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        })
      })
    }

  }
}
</script>

<style lang="scss" scoped>
.tab-container {
  $item-width: 200px;
  padding: 15px;
  .aside {
    min-width: 180px;
    background-color: white;
  }
  .main {
    .main-search {
      .form-item {
        width: $item-width;
      }
    }
    .main-report {
      margin-top: 10px;
    }
  }
}
</style>
